// 2550_microbunkou
#include "./USB/usb_function_cdc.h"
#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "usb_config.h"
#include "USB/usb_device.h"
#include "USB/usb.h"
#include "HardwareProfile.h"
#include <delays.h>
#include <timers.h>
#include <adc.h>
//#include <stdio.h>
#include <i2c.h>
#include <math.h>
#pragma config FOSC=HSPLL_HS, PLLDIV=5, CPUDIV=OSC1_PLL2
#pragma config USBDIV=2, PWRT=ON, FCMEN=ON, IESO=ON
#pragma config BOR=ON, BORV=0, WDT=OFF, LVP=OFF, VREGEN=ON
#pragma config MCLRE=ON, PBADEN=OFF, WDTPS=1024
#pragma config LPT1OSC=OFF, CCP2MX=OFF, DEBUG=OFF
#pragma config CP0=OFF, CP1=OFF, CP2=OFF, CP3=OFF, CPB=OFF
#pragma config CPD=OFF, WRT0=OFF, WRT1=OFF, WRT2=OFF, WRT3=OFF
#pragma config WRTB=OFF, WRTC=OFF, WRTD=OFF, EBTR0=OFF
#pragma config EBTR1=OFF, EBTR2=OFF, EBTR3=OFF, EBTRB=OFF
#define Delay_50uS Delay100TCYx(6)
#define Delay_100uS Delay100TCYx(12)
#define Delay_500uS Delay1KTCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)
const double pA0 = 3.098182303e2;
const double pB1 = 2.734323571;
const double pB2 =-1.647768389e-3;
const double pB3 =-4.018691578e-6;
const double pB4 =-3.208813535e-9;
const double pB5 = 2.091379510e-11;
#pragma udata
double V,VV,pix,wL;
unsigned int v,vv,ii,s,S,ck,pc,WL;
unsigned char j,i;
unsigned char W0,W1,W2,W3;
unsigned int R0,R1,R2,R3;
char buf[12];
char buff[12];
char input_buffer[64];
char output_buffer[64];
void main(void);
void YourHighPriorityISRCode();
void YourLowPriorityISRCode();
void ProcessIO(void);
void UserInit(void);
#pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = 0x08
void Remapped_High_ISR (void){
_asm goto YourHighPriorityISRCode _endasm
}
#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = 0x18
void Remapped_Low_ISR (void){
_asm goto YourLowPriorityISRCode _endasm
}
#pragma code
#pragma interrupt YourHighPriorityISRCode
void YourHighPriorityISRCode(){
USBDeviceTasks();
}
#pragma interruptlow YourLowPriorityISRCode
void YourLowPriorityISRCode(){
}
#pragma code
// 1バイト書き込み関数
unsigned char EByteWrite( unsigned char control,
unsigned int address, unsigned char data ){
IdleI2C();
SSPCON2bits.SEN = 1;
while ( SSPCON2bits.SEN );
if ( PIR2bits.BCLIF ){
return ( -1 );
} else {
SSPBUF = control;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-2);
}
PIR1bits.SSPIF = 0;
SSPBUF = address >> 8;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-3);
}
PIR1bits.SSPIF = 0;
SSPBUF = address;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-4);
}
PIR1bits.SSPIF = 0;
SSPBUF = data;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-5);
}
PIR1bits.SSPIF = 0;
SSPCON2bits.PEN = 1;
while ( SSPCON2bits.PEN );
PIR1bits.SSPIF = 0;
if ( PIR2bits.BCLIF ){
return ( -1 );
}
return ( 0 );
}
}
// 1バイト読み出し関数
unsigned int EByteRead( unsigned char control, unsigned int address ){
IdleI2C();
SSPCON2bits.SEN = 1;
while ( SSPCON2bits.SEN );
if ( PIR2bits.BCLIF ) {
return ( -1 );
} else {
SSPBUF = control;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-2);
}
PIR1bits.SSPIF = 0;
SSPBUF = address >> 8;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-3);
}
PIR1bits.SSPIF = 0;
SSPBUF = address;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-4);
}
PIR1bits.SSPIF = 0;
SSPCON2bits.RSEN = 1;
while ( SSPCON2bits.RSEN );
if ( PIR2bits.BCLIF ){
return ( -5 );
} else {
SSPBUF = control+1;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-6);
}
PIR1bits.SSPIF = 0;
SSPCON2bits.RCEN = 1;
while ( SSPCON2bits.RCEN );
SSPCON2bits.ACKDT = 1;
SSPCON2bits.ACKEN = 1;
while ( SSPCON2bits.ACKEN );
PIR1bits.SSPIF = 0;
SSPCON2bits.PEN = 1;
while ( SSPCON2bits.PEN );
if ( PIR2bits.BCLIF ){
return ( -7 );
} else {
PIR1bits.SSPIF = 0;
return ( (unsigned int) SSPBUF );
}
}
}
}
void UserInit(void){
ck = 1; pc = 0;
PORTAbits.RA4 = 0; // クロック L
PORTAbits.RA2 = 1; // ストローブ H
PORTCbits.RC7 = 0; // LED 消灯
for(i=0; i<12; i++) buf[i] = '0';
}
void main(void){
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTE = 0x00;
TRISA = 0b00000011; // AN0,1
TRISB = 0b00000000;
TRISC = 0b00000000;
ADCON0 = 0b00000001; // ADC ON
ADCON1 = 0b00001101; // AN0,1 analog, Vss-Vdd
ADCON2 = 0b10100110; // Right,12Tad,1/64
OpenI2C(MASTER, SLEW_ON);
SSPADD = 9;
UserInit();
USBDeviceInit();// USB初期化.
while(1){
SetChanADC(ADC_CH1); // ボリュームからの電圧をAD変換
ConvertADC();
while(BusyADC());
s = (ADRESH*256) + ADRESL;
S = s; // 感度レベル 1〜1000
if(S < 1){
S = 1;
}
if(S > 1000){
S = 1000;
}
/* USB接続完了待ち */
if(USB_BUS_SENSE && (USBGetDeviceState() == DETACHED_STATE))
USBDeviceAttach();
/* デバイス接続状態なら機能実行 */
if((USBDeviceState >= CONFIGURED_STATE)&&(USBSuspendControl!=1)){
ProcessIO();
}
}
}
void ProcessIO(void){
if(getsUSBUSART(input_buffer,20)){
switch(input_buffer[0]) { // 最初の1文字チェック
case '0':
for(j=0;j<2;j++){
if(USBUSARTIsTxTrfReady()) // 送信可能か?
putrsUSBUSART("OK");
CDCTxService();
}
break;
case '1':
for(ck=1; ck<382; ck++){// クロックサイクル = 288+6+87
PORTCbits.RC7 = 1; // LED 点灯
PORTAbits.RA4 = 1; // クロック H
for(ii=1; ii<S+1; ii++){ // クロックの長さにより感度調整
Delay10TCYx(6);
}
//Delay_1mS;
if(ck < 6){
PORTAbits.RA2 = 1; // ストローブ H
PORTAbits.RA4 = 0; // クロック L
for(ii=1; ii<S+1; ii++){
Delay10TCYx(6);
}
}
if((ck > 5)&&(ck < 94)){
PORTAbits.RA2 = 0; // ストローブ L のタイミングから88クロック目の立下りから データ送付開始
PORTAbits.RA4 = 0; // クロック L
for(ii=1; ii<S+1; ii++){
Delay10TCYx(6);
}
}
if((ck > 93)&&(pc < 288)){ // = 6+87
pc++;
PORTAbits.RA2 = 0;
PORTAbits.RA4 = 0; // クロック H→L のタイミングで ADCデータ採取
SetChanADC(ADC_CH0); // AD変換
ConvertADC();
while(BusyADC());
v = (ADRESH*256) + ADRESL;
vv = v;
buff[3] = (vv%10) + '0'; vv = vv/10; //電圧
buff[2] = (vv%10) + '0'; vv = vv/10;
buff[1] = (vv%10) + '0'; vv = vv/10;
buff[0] = (vv%10) + '0';
// 電圧データの記録
W0 = EByteWrite(0xA0,pc*4-4,buff[0]); Delay_5mS;
W1 = EByteWrite(0xA0,pc*4-3,buff[1]); Delay_5mS;
W2 = EByteWrite(0xA0,pc*4-2,buff[2]); Delay_5mS;
W0 = EByteWrite(0xA0,pc*4-1,buff[3]); Delay_5mS;
}
}
PORTCbits.RC7 = 0; // LED 消灯
pc = 0; ck = 1;
break;
case '2':
if(pc < 288){
pc++;
// 電圧データの読み出し
R0 = EByteRead(0xA0,pc*4-4);
R1 = EByteRead(0xA0,pc*4-3);
R2 = EByteRead(0xA0,pc*4-2);
R3 = EByteRead(0xA0,pc*4-1);
buf[0] = R0; buf[1] = R1; buf[2] = R2; buf[3] = R3;
WL = pc;
//pix = pc; // 型変換、 波長の補正計算
//wL = pA0 +pB1*pix +pB2*pow(pix,2) +pB3*pow(pix,3) +pB4*pow(pix,4) +pB5*pow(pix,5);
//WL = wL; // 型変換
buf[10] = (WL%10) + '0'; WL = WL/10;
buf[9] = (WL%10) + '0'; WL = WL/10;
buf[8] = (WL%10) + '0'; WL = WL/10;
buf[7] = (WL%10) + '0';
for(j=0; j<2; j++){
if(USBUSARTIsTxTrfReady())
putUSBUSART(buf, 12);
CDCTxService();
}
}
break;
case '3':
pc = 0; ck = 1;
break;
default:
break;
}
}
}
// ***********************************************************
// ************** USB Callback Functions *********************
// ***********************************************************
/*************************************************************
* Function: void USBCBSuspend(void)
*************************************************************/
void USBCBSuspend(void)
{
}
/*************************************************************
* Function: void USBCBWakeFromSuspend(void)
*************************************************************/
void USBCBWakeFromSuspend(void)
{
}
/*************************************************************
* Function: void USBCB_SOF_Handler(void)
*************************************************************/
void USBCB_SOF_Handler(void)
{
}
/*************************************************************
* Function: void USBCBErrorHandler(void)
*************************************************************/
void USBCBErrorHandler(void)
{
}
/*************************************************************
* Function: void USBCBCheckOtherReq(void)
*************************************************************/
void USBCBCheckOtherReq(void)
{
USBCheckCDCRequest();
}//end
/*************************************************************
* Function: void USBCBStdSetDscHandler(void)
*************************************************************/
void USBCBStdSetDscHandler(void)
{
}//end
/*************************************************************
* Function: void USBCBInitEP(void)
*************************************************************/
void USBCBInitEP(void)
{
CDCInitEP();
}
/*************************************************************
* Function: void USBCBSendResume(void)
*************************************************************/
void USBCBSendResume(void)
{
static WORD delay_count;
if(USBGetRemoteWakeupStatus() == TRUE)
{
//Verify that the USB bus is in fact suspended, before we send
//remote wakeup signalling.
//if(USBIsBusSuspended() == TRUE)
{
USBMaskInterrupts();
//Clock switch to settings consistent with normal USB operation.
USBCBWakeFromSuspend();
USBSuspendControl = 0;
//USBBusIsSuspended = FALSE; //So we don't execute this code again,
//until a new suspend condition is detected.
delay_count = 3600U;
do
{
delay_count--;
}while(delay_count);
//Now drive the resume K-state signalling onto the USB bus.
USBResumeControl = 1; // Start RESUME signaling
delay_count = 1800U; // Set RESUME line for 1-13 ms
do
{
delay_count--;
}while(delay_count);
USBResumeControl = 0; //Finished driving resume signalling
USBUnmaskInterrupts();
}
}
}
/***************************************************************
* Function: void USBCBEP0DataReceived(void)
***************************************************************/
#if defined(ENABLE_EP0_DATA_RECEIVED_CALLBACK)
void USBCBEP0DataReceived(void)
{
}
#endif
/***************************************************************
* Function: BOOL USER_USB_CALLBACK_EVENT_HANDLER(
***************************************************************/
BOOL USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, WORD size)
{
switch(event)
{
case EVENT_TRANSFER:
break;
case EVENT_SOF:
USBCB_SOF_Handler();
break;
case EVENT_SUSPEND:
USBCBSuspend();
break;
case EVENT_RESUME:
USBCBWakeFromSuspend();
break;
case EVENT_CONFIGURED:
USBCBInitEP();
break;
case EVENT_SET_DESCRIPTOR:
USBCBStdSetDscHandler();
break;
case EVENT_EP0_REQUEST:
USBCBCheckOtherReq();
break;
//case EVENT_BUS_ERROR:
//USBCBErrorHandler();
//break;
//case EVENT_TRANSFER_TERMINATED:
//break;
default:
break;
}
return TRUE;
}